home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / srgp / src / srgp_can.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  7.5 KB  |  296 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3.  
  4. #ifdef THINK_C
  5. #include "ChooseWhichQuickDraw.h"
  6. #endif
  7.  
  8. /** ABOUT CANVASES
  9. At any given time, there is one ACTIVE canvas.
  10.  
  11. The ACTIVE canvas is the one affected by all output primitives.
  12.  
  13. Initially, canvas #0 is the only canvas; it is always visible and
  14.    is the size of the screen.  Its coordinate system is the
  15.    one involved in all input commands.
  16.  
  17. The permanent spec for each canvas is stored in a
  18.    table (srgp__canvasTable).
  19. But the spec for the currently active canvas is
  20.    stored in a cache (srgp__curActiveCanvasSpec) and sometimes
  21.    is more current than the permanent version.
  22. **/
  23.  
  24.  
  25.  
  26.  
  27. /** INTERNAL: setting canvas defaults
  28. Sets the defaults for the currently active canvas.
  29. Affects the cache (srgp__curActiveCanvasSpec) first.
  30. Then copies into the permanent table.
  31. Assumes that "max_coord" fields are already set.
  32. **/
  33.  
  34. void
  35. SRGP__setCanvasDefaults ()
  36. {
  37.    PUSH_TRACE;
  38.  
  39.    /* Fool the attribute routines so they *have* to do the work. */
  40.    memset (&srgp__curActiveCanvasSpec.attributes, -1,
  41.            sizeof(srgp__attribute_group));
  42.  
  43.    SRGP_setWriteMode (WRITE_REPLACE);
  44.    SRGP_setClipRectangle
  45.       (SRGP_defRectangle
  46.          (0,0,
  47.           srgp__curActiveCanvasSpec.max_xcoord,
  48.           srgp__curActiveCanvasSpec.max_ycoord));
  49.  
  50.    SRGP_setFont (0);
  51.    SRGP_setLineStyle (CONTINUOUS);
  52.    SRGP_setLineWidth (1);
  53.    SRGP_setMarkerStyle (MARKER_CIRCLE);
  54.    SRGP_setMarkerSize (10);
  55.    SRGP_setFillStyle (SOLID);
  56.    SRGP_setPenStyle (SOLID);
  57.    SRGP_setFillBitmapPattern (0);
  58.    SRGP_setFillPixmapPattern (0);
  59.    SRGP_setPenBitmapPattern (0);
  60.    SRGP_setPenPixmapPattern (0);
  61.  
  62.    /* ERASE CANVAS MANUALLY. */
  63. #ifndef GRX
  64.    SRGP_setColor (0);
  65. #else
  66.    SRGP_setColor(SRGP_WHITE);
  67. #endif
  68.    SRGP_fillRectangleCoord
  69.       (0,0,
  70.        srgp__curActiveCanvasSpec.max_xcoord,
  71.        srgp__curActiveCanvasSpec.max_ycoord);
  72. #ifndef GRX
  73.    SRGP_setColor (1);
  74.    SRGP_setBackgroundColor (0);
  75. #else
  76.    SRGP_setColor (SRGP_BLACK);
  77.    SRGP_setBackgroundColor (SRGP_WHITE);
  78. #endif
  79.  
  80.    /* MAKE SURE CHANGES GET INTO THE ACTUAL TABLE (not just in the cache) */
  81. #ifdef GRX
  82.    GrSaveContext(&srgp__curActiveCanvasSpec.grx_canvas);
  83. #endif
  84.    srgp__canvasTable[srgp__curActiveCanvasId] = srgp__curActiveCanvasSpec;
  85.  
  86.    POP_TRACE;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. /*!*/
  93. void
  94. SRGP_useCanvas (canvasID canvas_id)
  95. {
  96.    DEBUG_AIDS{
  97.       SRGP_trace (SRGP_logStream, "SRGP_useCanvas: %d\n", canvas_id);
  98.       srgp_check_system_state();
  99.       srgp_check_extant_canvas(canvas_id);
  100.       LeaveIfNonFatalErr();
  101.    }
  102.  
  103.    /* UPDATE PERMANENT COPY OF SPEC FOR CURRENT ACTIVE CANVAS. */
  104.    srgp__canvasTable[srgp__curActiveCanvasId] = srgp__curActiveCanvasSpec;
  105.  
  106.    /* SET NEW ACTIVE, and LOAD CACHE. */
  107.    srgp__curActiveCanvasId = canvas_id;
  108.    srgp__curActiveCanvasSpec = srgp__canvasTable[srgp__curActiveCanvasId];
  109.  
  110. #ifdef THINK_C
  111.    SetPort (srgp__canvasTable[srgp__curActiveCanvasId].drawable.win);
  112. #endif
  113. #ifdef GRX
  114.    GrSetContext(&srgp__curActiveCanvasSpec.grx_canvas);
  115. #endif
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /*!*/
  122. canvasID
  123. SRGP_createCanvas (int width, int height)
  124. {
  125.    register int new_canvas_id;
  126.    boolean free_one_found;
  127.    canvas_spec *canvsptr;
  128.  
  129.    DEBUG_AIDS{
  130.       SRGP_trace (SRGP_logStream, "SRGP_createCanvas: %d %d\n", width, height);
  131.       srgp_check_system_state();
  132.       LeaveIfNonFatalErr();
  133.    }
  134.  
  135.    /* FIND AN EMPTY ENTRY IN CANVAS TABLE. */
  136.    new_canvas_id = 1;
  137.    free_one_found = FALSE;
  138.    while ((new_canvas_id <= MAX_CANVAS_INDEX) && !free_one_found)
  139.       if (srgp__canvasTable[new_canvas_id].drawable.bitmap != 0)
  140.          new_canvas_id++;
  141.       else
  142.          free_one_found = TRUE;
  143.  
  144.    if (!free_one_found) {
  145.       SRGP__error (ERR_CANVAS_TABLE_FULL);
  146.       return (-1);
  147.    }
  148.  
  149.    /* ALL SYSTEMS ARE GO! */
  150.    canvsptr = &(srgp__canvasTable[new_canvas_id]);
  151.  
  152.    /* ALLOCATE THE BITMAP */
  153. #ifdef X11
  154.    if ( (canvsptr->drawable.bitmap =
  155.          XCreatePixmap (srgpx__display,
  156.                         srgp__canvasTable[0].drawable.win,
  157.                         width, height,
  158.                         srgp__available_depth))
  159.                                         == 0) {
  160.       SRGP__error (ERR_MALLOC);
  161.       return (-1);
  162.    }
  163. #endif
  164.  
  165. #ifdef THINK_C
  166. #ifdef COLOR_QUICKDRAW
  167. {
  168.    CGrafPtr cgptr;
  169.    int depth, rowBytes;
  170.    long size;
  171.    Rect bounds;
  172.    Ptr myBits;
  173.  
  174.    SetRect (&bounds, 0, 0, width, height);
  175.    OpenCPort (cgptr = canvsptr->drawable.xid =
  176.               (CGrafPtr)(NewPtr(sizeof(CGrafPort))));
  177.    depth = (**(*cgptr).portPixMap).pixelSize;
  178.    rowBytes = (((depth * width) + 15) >> 4) << 1;
  179.    size = (long) height * rowBytes;
  180.    if ((myBits = NewPtr(size)) == NULL) {
  181.       SRGP__error (ERR_MALLOC);
  182.       return (-1);
  183.    }
  184.    (**(*cgptr).portPixMap).baseAddr = myBits;
  185.    (**(*cgptr).portPixMap).rowBytes = rowBytes + 0x8000;
  186.    (**(*cgptr).portPixMap).bounds = bounds;
  187. }
  188.  
  189. #else
  190.  
  191. {
  192.    GrafPtr cgptr;
  193.    int rowBytes;
  194.    long memreq;
  195.    Rect bounds;
  196.    BitMap newBitMap;
  197.    Ptr myBits;
  198.  
  199.    SetRect (&bounds, 0, 0, width, height);
  200.    OpenPort (cgptr = canvsptr->drawable.xid =
  201.              (GrafPtr)(NewPtr(sizeof(GrafPort))));
  202.    newBitMap.rowBytes = (width>>3);
  203.    if ((width % 8) > 0)
  204.       newBitMap.rowBytes ++;
  205.    if ((rowBytes % 2) > 0)
  206.       newBitMap.rowBytes++;
  207.    SetRect (&newBitMap.bounds, 0, 0, width, height);
  208.    memreq = newBitMap.rowBytes * height;
  209.    if ((newBitMap.baseAddr = NewPtr(memreq)) == NULL) {
  210.       SRGP__error (ERR_MALLOC);
  211.       return (-1);
  212.    }
  213.    else {
  214.       SetPortBits (&newBitMap);
  215.       thePort->portRect = newBitMap.bounds;
  216.    }
  217. }
  218. #endif
  219. #endif
  220.  
  221. #ifdef GRX
  222.    canvsptr->drawable.bitmap =
  223.         GrCreateContext(width,height,NULL,&canvsptr->grx_canvas);
  224.    if(canvsptr->drawable.bitmap == NULL) {
  225.         SRGP__error(ERR_MALLOC);
  226.         return(-1);
  227.    }
  228. #endif
  229.  
  230.  
  231.    /* SET VARIOUS IMPORTANT FIELDS IN THE CANVAS SPEC. */
  232.    canvsptr->max_xcoord = width-1;
  233.    canvsptr->max_ycoord = height-1;
  234.  
  235.  
  236. #ifdef X11
  237.    canvsptr->gc_fill =
  238.       XCreateGC (srgpx__display, canvsptr->drawable.bitmap, 0L, NULL);
  239.    canvsptr->gc_frame =
  240.       XCreateGC (srgpx__display, canvsptr->drawable.bitmap, 0L, NULL);
  241. #endif
  242.  
  243.  
  244.    /* TELL THE WORLD TO PREPARE TO OUTPUT TO NEW BITMAP */
  245.    PUSH_TRACE;
  246.    SRGP_useCanvas (new_canvas_id);
  247.    SRGP__setCanvasDefaults();
  248.    POP_TRACE;
  249.  
  250.    return new_canvas_id;
  251. }
  252.  
  253.  
  254.  
  255.  
  256. /*!*/
  257. void
  258. SRGP_deleteCanvas (canvasID canvas_id)
  259. {
  260.    DEBUG_AIDS {
  261.       SRGP_trace (SRGP_logStream, "SRGP_deleteCanvas: %d\n", canvas_id);
  262.       srgp_check_system_state();
  263.       srgp_check_extant_canvas(canvas_id);
  264.       LeaveIfNonFatalErr();
  265.    }
  266.  
  267.    if (canvas_id == SCREEN_CANVAS)
  268.       SRGP__error (ERR_DELETION_OF_SCREEN);
  269.    if (canvas_id == srgp__curActiveCanvasId)
  270.       SRGP__error (ERR_DELETION_OF_ACTIVE_CANVAS);
  271.    LeaveIfNonFatalErr();
  272.  
  273. #ifdef X11
  274.    XFreePixmap (srgpx__display, srgp__canvasTable[canvas_id].drawable.bitmap);
  275. #endif
  276.  
  277. #ifdef THINK_C
  278. #ifdef COLOR_QUICKDRAW
  279. #  define canvasGrafport (*((CGrafPtr)(srgp__canvasTable[canvas_id].drawable.xid)))
  280.    DisposPtr ((**(canvasGrafport.portPixMap)).baseAddr);
  281. #else
  282. #  define canvasGrafport (*((GrafPtr)(srgp__canvasTable[canvas_id].drawable.xid)))
  283.    DisposPtr (canvasGrafport.portBits.baseAddr);
  284. #endif
  285.    ClosePort (srgp__canvasTable[canvas_id].drawable.xid);
  286.    DisposPtr (srgp__canvasTable[canvas_id].drawable.xid);
  287. #endif
  288.  
  289. #ifdef GRX
  290.    GrDestroyContext(&srgp__canvasTable[canvas_id].grx_canvas);
  291. #endif
  292.  
  293.    srgp__canvasTable[canvas_id].drawable.bitmap = 0; /* FREES THE TABLE ENTRY */
  294. }
  295.  
  296.